---
title: "Figure A.9"
output: 
---

# Figure A.9

# Who's to Blame? Postconflict Violence and Public Attitudes Towards Peace Agreements
# Wyer, Frank. 

#clear environment
```{r clear environment}
rm(list = ls())
```

# uncomment and set working directory to replication archive
# setwd("~/blame_replication")

# Uncomment to install packages if necessary
# install.packages("tidyverse")
# install.packages("estimatr")
# install.packages("fabricatr")
# install.packages("cowplot")

#load packages
```{r}
library(tidyverse)
library(estimatr)
library(fabricatr)
library(cowplot)
```

#read in data
```{r}
survey_clean <- read.csv("survey_clean.csv")
intensity_data <- read.csv("Raw Data Files/intensity_data.csv")
displace_data <- read.csv("Raw Data Files/displace_data.csv")
```

#merge data on conflict displacement and intensity to survey data
```{r}
survey_clean <- left_join(survey_clean, intensity_data, by = "municode")
survey_clean <- left_join(survey_clean, displace_data, by = "municode")
```

#split municipalities into categories based on displacement variable
```{r displacement categorical variable}
survey_clean <- survey_clean %>% mutate(displace_pre_hilo = as.factor(split_quantile(displace_rate_pre, 2)))
survey_clean <- survey_clean %>% mutate(displace_post_hilo = as.factor(split_quantile(displace_rate_post, 2)))
```
 
#split municipalities into categories based on conflict intensity variable
```{r intensity categorical variable}
survey_clean <- survey_clean %>% mutate(victims_post_hilo = as.factor(split_quantile(victims_rate_post, 2)), victims_pre_hilo = as.factor(split_quantile(victims_rate_pre, 2)))
```

#het effects: high conflict displacement pre 2016
```{r het effects:  displacement pre}
m_displace_high_pre <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + farc_presence + homratediff + urbandummy + Q25 + engage_zscale + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(displace_pre_hilo == 2), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(displace = "Higher Displacement")
m_displace_low_pre <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + farc_presence + homratediff + urbandummy + Q25 + engage_zscale + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(displace_pre_hilo == 1), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(displace = "Lower Displacement")

het_displace_pre_df <- rbind(m_displace_high_pre, m_displace_low_pre)

het_displace_pre_df <- het_displace_pre_df %>% mutate(Treatment = case_when(
term == "treatmentT1" ~ "Postconflict Violence",
term == "treatmentT2B" ~ "Rebel Culpability",
term == "treatmentT2A" ~ "Govt. Culpability",
))

het_displace_pre_df$Treatment <- factor(het_displace_pre_df$Treatment, levels = c("Postconflict Violence", "Rebel Culpability", "Govt. Culpability"))

displace_pre_het_plot <- ggplot(het_displace_pre_df,  aes(x = estimate, y = displace)) +
    geom_point(position=position_dodge(.5), shape = 15) +
    geom_linerange(aes(y = displace, xmin = conf.low, xmax = conf.high), position=position_dodge(.5)) +
    labs(x = "", y = "", title = "Conflict Displacement Pre-2016") +
    theme_bw() + 
    xlim(-1.5,1.5) +
    scale_y_discrete(labels = function(x) str_wrap(x, width = 4)) +
    theme(plot.title = element_text(face = "bold", size = 10)) +
    facet_grid(Treatment ~ ., labeller = label_wrap_gen(width = 15, multi_line = TRUE))
```


#het effects: high conflict displacement post 2016
```{r het effects:  displacement pre}
m_displace_high_post <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + farc_presence + homratediff + urbandummy + Q25 + engage_zscale + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(displace_post_hilo == 2), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(displace = "Higher Displacement")
m_displace_low_post <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + farc_presence + homratediff + urbandummy + Q25 + engage_zscale + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(displace_post_hilo == 1), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(displace = "Lower Displacement")

het_displace_post_df <- rbind(m_displace_high_post, m_displace_low_post)

het_displace_post_df <- het_displace_post_df %>% mutate(Treatment = case_when(
term == "treatmentT1" ~ "Postconflict Violence",
term == "treatmentT2B" ~ "Rebel Culpability",
term == "treatmentT2A" ~ "Govt. Culpability",
))

het_displace_post_df$Treatment <- factor(het_displace_post_df$Treatment, levels = c("Postconflict Violence", "Rebel Culpability", "Govt. Culpability"))

displace_post_het_plot <- ggplot(het_displace_post_df,  aes(x = estimate, y = displace)) +
    geom_point(position=position_dodge(.5), shape = 15) +
    geom_linerange(aes(y = displace, xmin = conf.low, xmax = conf.high), position=position_dodge(.5)) +
    labs(x = "", y = "", title = "Conflict Displacement Post-2016") +
    theme_bw() + 
    xlim(-1.5,1.5) +
    scale_y_discrete(labels = function(x) str_wrap(x, width = 4)) +
    theme(plot.title = element_text(face = "bold", size = 10)) +
    facet_grid(Treatment ~ ., labeller = label_wrap_gen(width = 15, multi_line = TRUE))
```

#het effects: high incidence of conflict intensity pre 2016
```{r het effects:  displacement pre}
m_intensity_high_pre <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + farc_presence + homratediff + urbandummy + Q25 + engage_zscale + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(victims_pre_hilo == 2), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(intensity = "Higher Intensity")
m_intensity_low_pre <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + farc_presence + homratediff + urbandummy + Q25 + engage_zscale + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(victims_pre_hilo == 1), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(intensity = "Lower Intensity")

het_intensity_pre_df <- rbind(m_intensity_high_pre, m_intensity_low_pre)

het_intensity_pre_df <- het_intensity_pre_df %>% mutate(Treatment = case_when(
term == "treatmentT1" ~ "Postconflict Violence",
term == "treatmentT2B" ~ "Rebel Culpability",
term == "treatmentT2A" ~ "Govt. Culpability",
))

het_intensity_pre_df$Treatment <- factor(het_intensity_pre_df$Treatment, levels = c("Postconflict Violence", "Rebel Culpability", "Govt. Culpability"))

intensity_pre_het_plot <- ggplot(het_intensity_pre_df,  aes(x = estimate, y = intensity)) +
    geom_point(position=position_dodge(.5), shape = 15) +
    geom_linerange(aes(y = intensity, xmin = conf.low, xmax = conf.high), position=position_dodge(.5)) +
    labs(x = "", y = "", title = "Conflict Intensity Pre-2016") +
    theme_bw() + 
    xlim(-1.5,1.5) +
    scale_y_discrete(labels = function(x) str_wrap(x, width = 4)) +
    theme(plot.title = element_text(face = "bold", size = 10)) +
    facet_grid(Treatment ~ ., labeller = label_wrap_gen(width = 15, multi_line = TRUE))
```

#het effects: high incidence of conflict intensity post 2016
```{r het effects:  intensity post}
m_intensity_high_post <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + farc_presence + homratediff + urbandummy + Q25 + engage_zscale + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(victims_post_hilo == 2), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(intensity = "Higher Intensity")
m_intensity_low_post <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + farc_presence + homratediff + urbandummy + Q25 + engage_zscale + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(victims_post_hilo == 1), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(intensity = "Lower Intensity")

het_intensity_post_df <- rbind(m_intensity_high_post, m_intensity_low_post)

het_intensity_post_df <- het_intensity_post_df %>% mutate(Treatment = case_when(
term == "treatmentT1" ~ "Postconflict Violence",
term == "treatmentT2B" ~ "Rebel Culpability",
term == "treatmentT2A" ~ "Govt. Culpability",
))

het_intensity_post_df$Treatment <- factor(het_intensity_post_df$Treatment, levels = c("Postconflict Violence", "Rebel Culpability", "Govt. Culpability"))

intensity_post_het_plot <- ggplot(het_intensity_post_df,  aes(x = estimate, y = intensity)) +
    geom_point(position=position_dodge(.5), shape = 15) +
    geom_linerange(aes(y = intensity, xmin = conf.low, xmax = conf.high), position=position_dodge(.5)) +
    labs(x = "", y = "", title = "Conflict Intensity Post-2016") +
    theme_bw() + 
    xlim(-1.5,1.5) +
    scale_y_discrete(labels = function(x) str_wrap(x, width = 4)) +
    theme(plot.title = element_text(face = "bold", size = 10)) +
    facet_grid(Treatment ~ ., labeller = label_wrap_gen(width = 15, multi_line = TRUE))
```

#create combined plot with all conflict exposure measures
```{r combined conflict exposure plot}
intense_plot_all <- plot_grid(intensity_pre_het_plot, intensity_post_het_plot, displace_pre_het_plot, displace_post_het_plot, nrow = 2, ncol = 2)
```

